package com.shrio.word.poi2;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.Rows;
import com.deepoove.poi.util.PoitlIOUtils;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.*;
import java.util.*;

public class PreTestExport {

    public static void main(String[] args) throws Exception{
        // 获取模板文件流
//        InputStream resourceAsStream = new FileInputStream(new File("E:/word/poi-tl-pre-old2.docx"));
        InputStream resourceAsStream = new FileInputStream(new File("E:/word/poi-tl-template.docx"));
        //poi-tl 配置
        ConfigureBuilder builder = Configure.builder();
        builder.useSpringEL(false);

        Map<String,Object> map = new HashMap<String,Object>();

        // 伪造一个表格数据
        //单个表格
        ServerTableData oneTable = getServerTableData();
        map.put("oneTable",oneTable);
        map.put("title", "2023");
        builder.bind("oneTable",new ServerTablePolicy());


        XWPFTemplate template = XWPFTemplate.compile(Objects.requireNonNull(resourceAsStream), builder.build()).render(map);
        // 获取表格对象
        XWPFTable table = template.getXWPFDocument().getTableArray(0);

        // 设置表格边框样式为黑色实线
        CTTblPr tblPr = table.getCTTbl().getTblPr();
        CTTblBorders tblBorders = tblPr.isSetTblBorders() ? tblPr.getTblBorders() : tblPr.addNewTblBorders();
        CTBorder border = tblBorders.addNewTop();
        border.setVal(STBorder.SINGLE);
        border.setColor("000000");

        // HttpServletResponse response
        OutputStream out = new FileOutputStream(new File("E:/word/poi-tl-降水new.docx"));
        BufferedOutputStream bos = new BufferedOutputStream(out);
        template.write(bos);
        bos.flush();
        out.flush();
        PoitlIOUtils.closeQuietlyMulti(template, bos, out);

    }

    private static ServerTableData getServerTableData() {
        ServerTableData serverTableData = new ServerTableData();
        List<RowRenderData> serverDataList = new ArrayList<RowRenderData>();
        RowRenderData serverData1 = Rows.of("广州", "天河", "0.3").textFontSize(14).center().create();
        RowRenderData serverData2 = Rows.of("广州", "白云", "0.2").textFontSize(14).center().create();
        RowRenderData serverData3 = Rows.of("广州", "东山", "0.1").textFontSize(14).center().create();
        serverDataList.add(serverData1);
        serverDataList.add(serverData2);
        serverDataList.add(serverData3);

        RowRenderData fs = Rows.of("佛山", "禅城", "0.3").textFontSize(14).center().create();
        serverDataList.add(fs);

        RowRenderData sz1 = Rows.of("深圳", "南山", "0.3").textFontSize(14).center().create();
        RowRenderData sz2 = Rows.of("深圳", "福田", "0.3").textFontSize(14).center().create();
        serverDataList.add(sz1);
        serverDataList.add(sz2);

        List<Map<String, Object>> groupDataList = new ArrayList<Map<String, Object>>();
        Map<String, Object> groupData1 = new HashMap<String, Object>();
        groupData1.put("typeName", "广州");
        groupData1.put("listSize", "3");
        Map<String, Object> groupData2 = new HashMap<String, Object>();
        groupData2.put("typeName", "深圳");
        groupData2.put("listSize", "2");

        Map<String, Object> groupData3 = new HashMap<String, Object>();
        groupData3.put("typeName", "佛山");
        groupData3.put("listSize", "1");

        groupDataList.add(groupData1);
        groupDataList.add(groupData2);
        groupDataList.add(groupData3);

        serverTableData.setServerDataList(serverDataList);
        serverTableData.setGroupDataList(groupDataList);
        serverTableData.setMergeColumn(0);
        return serverTableData;
    }

}
